﻿using System;
using System.Collections.Generic;
using Utilities;
using SocialShare.BusinessEntities;
using System.Linq;
using SocialShare.BLL;
using System.Globalization;

namespace SocialShare.BusinessLogicLayer
{

    internal partial class SocailShare_Forum_MessagesBLL
    {
        internal List<Message> GetAllForumMessages(Int32? categoryId)
        {

            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                    cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                 on
                    messages.CategoryId equals cat.CategoryId
                where
                    messages.ParentPostId == null
                orderby messages.IsAdminNotice descending, messages.PostId descending
                select new
                {
                    messages.PostId,
                    messages.CategoryId,
                    messages.CreateDate,
                    messages.MessageBody,
                    messages.MessageSubject,
                    messages.ParentPostId,
                    messages.Status,
                    messages.UserId,
                    cat.CategoryName,
                    Reply = (from m in _socialShareDatabaseContext.SocailShare_Forum_Messages where m.ParentPostId == messages.PostId select m.PostId).Count()
                };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName = x.CategoryName,
                TotalReply = x.Reply

            });
        }

        internal Int32 GetTotalCountAllActiveForumMessages(Int32? categoryId, Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32))
        {
            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                    cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                 on
                    messages.CategoryId equals cat.CategoryId
                where
                    messages.Status == (Int32)ForumStatus.Published && messages.ParentPostId == null
                orderby messages.IsAdminNotice descending, messages.CreateDate descending
                select new
                {
                    messages.PostId
                };

            return result.ToList().Count;

        }
       
        internal List<Message> GetAllActiveForumMessages(Int32? categoryId, Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32))
        {

            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                    cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                 on 
                    messages.CategoryId equals cat.CategoryId
                where
                    messages.Status == (Int32)ForumStatus.Published && messages.ParentPostId == null
                orderby messages.IsAdminNotice descending, messages.CreateDate descending
                select new
                {
                    messages.PostId,
                    messages.CategoryId,
                    messages.CreateDate,
                    messages.MessageBody,
                    messages.MessageSubject,
                    messages.ParentPostId,
                    messages.Status,
                    messages.UserId,
                    cat.CategoryName,
                    Reply = (from m in _socialShareDatabaseContext.SocailShare_Forum_Messages where m.ParentPostId == messages.PostId select m.PostId).Count()
                };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName=x.CategoryName,
                TotalReply=x.Reply
            }).Skip(startRowIndex).Take(maximumRows).ToList();
        }

        internal Int32 GetTotalCountActiveForumMessagesByCategory(Int32? categoryId, Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32))
        {
            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                    cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                 on
                    messages.CategoryId equals cat.CategoryId
                where
                    messages.Status == (Int32)ForumStatus.Published && messages.ParentPostId == null && messages.CategoryId == categoryId
                orderby messages.IsAdminNotice descending, messages.CreateDate descending
                select new
                {
                    messages.PostId
                };

            return result.ToList().Count;
        }

        internal List<Message> GetAllActiveForumMessagesByCategory(Int32? categoryId, Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32))
        {

            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                    cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                 on
                    messages.CategoryId equals cat.CategoryId
                where
                    messages.Status == (Int32)ForumStatus.Published && messages.ParentPostId == null && messages.CategoryId == categoryId
                orderby messages.IsAdminNotice descending, messages.CreateDate descending
                select new
                {
                    messages.PostId,
                    messages.CategoryId,
                    messages.CreateDate,
                    messages.MessageBody,
                    messages.MessageSubject,
                    messages.ParentPostId,
                    messages.Status,
                    messages.UserId,
                    cat.CategoryName,
                    Reply = (from m in _socialShareDatabaseContext.SocailShare_Forum_Messages where m.ParentPostId == messages.PostId select m.PostId).Count()
                };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName = x.CategoryName,
                TotalReply = x.Reply

            }).Skip(startRowIndex).Take(maximumRows).ToList(); ;
        }

        internal List<Message> GetAllForumMessagesByID(Int32 postId)
        {
            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                   cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                on
                   messages.CategoryId equals cat.CategoryId
                where
                    (messages.ParentPostId == postId || messages.PostId == postId)
                orderby messages.IsAdminNotice descending, messages.CreateDate descending
                select new
                {
                    messages.PostId,
                    messages.CategoryId,
                    messages.CreateDate,
                    messages.MessageBody,
                    messages.MessageSubject,
                    messages.ParentPostId,
                    messages.Status,
                    messages.UserId,
                    cat.CategoryName
                };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName = x.CategoryName
            });
        }

        internal List<Message> GetAllActiveForumMessagesByID(Int32 postId)
        {
            var result =
                from
                    messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
                join
                   cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
                on
                   messages.CategoryId equals cat.CategoryId
                where
                    messages.Status == (Int32)ForumStatus.Published && (messages.ParentPostId == postId || messages.PostId==postId)

                select new
                {
                    messages.PostId,
                    messages.CategoryId,
                    messages.CreateDate,
                    messages.MessageBody,
                    messages.MessageSubject,
                    messages.ParentPostId,
                    messages.Status,
                    messages.UserId,
                    cat.CategoryName
                };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName = x.CategoryName
            });
        }

        internal SocailShare_Forum_Messages LoadParentPost(Int32 postId)
        {
            SocailShare_Forum_Messages message = null;
            message = _socialShareDatabaseContext.SocailShare_Forum_Messages.FirstOrDefault(memObject => memObject.PostId == postId);

            if (message.ParentPostId.HasValue)
            {
                message = _socialShareDatabaseContext.SocailShare_Forum_Messages.FirstOrDefault(memObject => memObject.ParentPostId == postId);
            }

            return message;
        }

        internal List<MessageCategory> GetAllActiveForumCategory()
        {
            var result =
               from
                   cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
              
               select new
               {
                   cat.CategoryId,
                   cat.CategoryName,
                   cat.DateCreated,
                   Total = (from m in _socialShareDatabaseContext.SocailShare_Forum_Messages where m.CategoryId == cat.CategoryId && m.ParentPostId == null select m.CategoryId).Count(),
                   Reply = (from m in _socialShareDatabaseContext.SocailShare_Forum_Messages where m.CategoryId == cat.CategoryId && m.ParentPostId != null select m.CategoryId).Count(),
                   
               };

            
            return result.ToList().ConvertAll(x => new MessageCategory
            {
                CategoryName=x.CategoryName,
                CategoryId=x.CategoryId,
                TotalReply=x.Reply,
                TotalPost=x.Total
            });
        }

        internal List<CountReport> GetTotalPostsByMonth()
        {
            DateTimeFormatInfo info = new DateTimeFormatInfo();
            List<CountReport> reports = new List<CountReport>();
            var categoryCounts = (from p in _socialShareDatabaseContext.SocailShare_Forum_Messages group p by p.CreateDate.Month into g select new { Category = g.Key, ProductCount = g.Count() });
            for (Int32 index = 0; index < categoryCounts.ToList().Count; index++)
            {
                CountReport report = new CountReport();
                report.Caption = info.GetMonthName(categoryCounts.ToList()[index].Category);
                report.Total = categoryCounts.ToList()[index].ProductCount;
                reports.Add(report);
            }

            return reports;
        }

        internal int GetTotalCountAllPostsByMember(Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32), String UserID = default(String))
        {
            var result =
               from
                   messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
               join
                  cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
               on
                  messages.CategoryId equals cat.CategoryId
               where
                   messages.UserId == UserID

               select new
               {
                   messages.PostId,
                   messages.CategoryId,
                   messages.CreateDate,
                   messages.MessageBody,
                   messages.MessageSubject,
                   messages.ParentPostId,
                   messages.Status,
                   messages.UserId,
                   cat.CategoryName
               };

            return result.ToList().Count;

        }
        internal List<Message> GetAllPostsByMember(Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32), String UserID = default(String))
        {
            var result =
               from
                   messages in _socialShareDatabaseContext.SocailShare_Forum_Messages
               join
                  cat in _socialShareDatabaseContext.SocialShare_Forum_Categories
               on
                  messages.CategoryId equals cat.CategoryId
               where
                   messages.UserId == UserID

               select new
               {
                   messages.PostId,
                   messages.CategoryId,
                   messages.CreateDate,
                   messages.MessageBody,
                   messages.MessageSubject,
                   messages.ParentPostId,
                   messages.Status,
                   messages.UserId,
                   cat.CategoryName
               };

            return result.ToList().ConvertAll(x => new Message
            {
                PostId = x.PostId,
                CategoryId = x.CategoryId,
                CreateDate = x.CreateDate,
                MessageBody = x.MessageBody,
                MessageSubject = x.MessageSubject,
                ParentPostId = x.ParentPostId,
                Status = x.Status,
                UserId = x.UserId,
                CategoryName = x.CategoryName
            });
        }

    }
}
